package tp3.pages.java;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.WindowClosedCallback;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IStyledColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;

import tp1.Personaje;
import tp1_Model.Mapa;

public class VerPersonajesPage extends GeneralPage {
	public VerPersonajesPage() {
		final Form form = new Form("personajes");

		// WebMarkupContainer css = new WebMarkupContainer("mycss");
		// add(css);

		this.add(form);
		final ModalWindow modal = new ModalWindow("shengLong");
		modal.setContent(new ShengPanel("content"));
		modal.setWindowClosedCallback(new WindowClosedCallback() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			public void onClose(AjaxRequestTarget target) {
				target.add(form);
			}
		});
		add(modal);
		List<Personaje> personajes2 = Mapa.getInstance().getPersonajes();
		List<IStyledColumn> listaHeaders = new ArrayList<IStyledColumn>();
		listaHeaders.add(new PropertyColumn<Personaje>(new Model<String>("Nombre Personaje"), "nombre"));
		listaHeaders.add(new PropertyColumn<Personaje>(new Model<String>("Resistencia"), "resistencia"));
		listaHeaders.add(new PropertyColumn<Personaje>(new Model<String>("Cantidad de Esferas"), "cantidadDeEsferas"));

		AjaxFallbackDefaultDataTable personajes = new AjaxFallbackDefaultDataTable("verPersonajes", listaHeaders, new UserProvidrer(), 100);

		form.add(personajes);

		//
		List list = Mapa.getInstance().getPersonajesEnElMapa();
		ListView<Personaje> listview = new ListView<Personaje>("listview", list) {
			@Override
			protected void populateItem(ListItem<Personaje> item) {
				final Personaje per = item.getModelObject();
				item.add(new Label("nombre", per.getNombre()));
				item.add(new Image("imagen", per.getImage()));
				@SuppressWarnings("rawtypes")
				final AjaxLink ajaxLink = new AjaxLink("sheng") {
					@Override
					public boolean isEnabled() {
						return (per.cantidadDeEsferas() == 1);
					}

					@Override
					public void onClick(AjaxRequestTarget target) {
						per.delvolverEsferas();
						this.setEnabled(false);
						modal.show(target);

					}

				};
				item.add(ajaxLink);
			}
		};
		add(listview);

	}
}
